package com.quanquan.management.service;

import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.MarketingRulesDTO;
import com.quanquan.management.request.MarketingRulesRequestBody;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.Request;

import java.util.List;

/**
 * @ClassName:IntegralRuleController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/5/8 13:22
 * @Version 1.0
 */
@Service
public class MarketingRulesService {

    @Value("${module-url.userUrl}")
    private String userUrl;

    public List<MarketingRulesDTO> list() {
        MarketingRulesRequestBody marketingRulesRequestBody = new MarketingRulesRequestBody();
        String postJson = Request.postJson(userUrl + "marking_rule/list", marketingRulesRequestBody);
        JSONObject jsonObject = JSONObject.parseObject(postJson);
        Integer resCode = jsonObject.getInteger("resCode");
        List<MarketingRulesDTO> pageDTO = null;
        if (0 == resCode) {
            return jsonObject.getJSONArray("data").toJavaList(MarketingRulesDTO.class);
        }
        return pageDTO;
    }


    /**
     * 编辑
     *
     * @return
     */
    public MarketingRulesDTO getById(String id) {
        MarketingRulesRequestBody marketingRulesRequestBody = new MarketingRulesRequestBody();
        marketingRulesRequestBody.setId(id);
        String json = Request.postJson(userUrl + "marking_rule/find_by_id", marketingRulesRequestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        MarketingRulesDTO marketingRulesDTO = null;
        if (0 == resCode) {
            return jsonObject.getJSONObject("data").toJavaObject(MarketingRulesDTO.class);
        }
        return marketingRulesDTO;
    }

    /**
     * 编辑
     *
     * @return
     */
    public boolean update(MarketingRulesRequestBody requestBody) {
        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(userUrl + "marking_rule/update", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

    public boolean updateStatus(MarketingRulesRequestBody requestBody) {
        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(userUrl + "marking_rule/turn_on",requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 != resCode) {
            return false;
        }
        return true;
    }


}
